﻿//-----------------------------------------------------------------------
// <copyright file="Place.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BookCaching.Data.Storage;
using BookCaching.Localization;

namespace BookCaching.Data
{
    public class Place : IIsolatedStorage<Place>
    {
        #region Constructors
        public Place()
        {
        }

        public Place(Knihotoc_WCFService.Place wcfPlace)
        {
            if (wcfPlace != null)
            {
                this.ID = wcfPlace.ID;
                this.Name = wcfPlace.Name;
                this.Address = new Address(wcfPlace.Address);
                this.LogoImagePath = wcfPlace.ImageUrl;
            }
        }
        #endregion

        #region Public properties
        /// <summary>
        /// Gets or sets the ID of the place.
        /// </summary>
        public int ID { get; set; }
        
        /// <summary>
        /// Gets or sets the name of the place.
        /// </summary>
        public string Name { get; set; }
        
        /// <summary>
        /// Gets or sets the verbal description of the place.
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the address of the place.
        /// </summary>
        public Address Address { get; set; }

        /// <summary>
        /// Gets or sets the path to the logo of the place.
        /// </summary>
        public string LogoImagePath { get; set; }
        public Visibility LogoImageVisibility { get; set; }

        public double Distance { get; set; }
        public string DistanceString { get; set; }
        #endregion

        public bool SaveToIsolatedStorage()
        {
            bool everythingAdded = true;

            if (this.Name != null)
            {
                if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Place_" + this.ID.ToString() + "_Name", this.Name))
                {
                    everythingAdded = false;
                }
            }
            else
            {
                return false;
            }

            if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Place_" + this.ID.ToString() + "_Lat", this.Address.Lat.ToString()))
            {
                everythingAdded = false;
            }

            if (!IsolatedStorageHandler.Instance.AddOrUpdateValue("Place_" + this.ID.ToString() + "_Lon", this.Address.Lon.ToString()))
            {
                everythingAdded = false;
            }

            if (everythingAdded)
            {
                return true;
            }
            else
            {
                IsolatedStorageHandler.Instance.RemoveValue("Place_" + this.ID.ToString() + "_Name");
                IsolatedStorageHandler.Instance.RemoveValue("Place_" + this.ID.ToString() + "_Lat");
                IsolatedStorageHandler.Instance.RemoveValue("Place_" + this.ID.ToString() + "_Lon");

                return false;
            }
        }

        public void LoadFromIsolatedStorage(string id)
        {
            this.Name = IsolatedStorageHandler.Instance.GetValueOrDefault<string>(
                "Place_" + id + "_Name", LocalizedStrings.Get("NameNotFound"));
        }
    }
}
